﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using BaBaoFan.Modifier.Model;
using Dapper;

namespace BaBaoFan.Modifier.Data
{
    public class CoinInfoRepository : ICoinInfoRepository
    {
        private IDbConnection _conn;
        public CoinInfoRepository(DbConnection conn)
        {
            _conn = conn.Connection;
        }

        public bool Add(CoinInfo coinInfoInput)
        {
            string sql = @"insert into coin_type (coin_name,coin_device,coin_short,coin_time,coin_unit,coin_weight,coin_isexcavate,coin_sort,coin_Algorithm) 
                                                            values (@CoinName,@CoinDevice,@CoinShort,@CoinTime,@CoinUnit,@CoinWeight,@CoinIsExcavate,@CoinSort,@AlgorithmId)";
            return _conn.Execute(sql, coinInfoInput) > 0;
        }

        public CoinInfo GetCoinInfoById(int id)
        {
        string sql = @"select
                                c.id as Id,
                                c.coin_name as CoinName,
                                c.coin_device as CoinDevice,
                                c.coin_short as CoinShort,
                                c.coin_time as CoinTime,
                                c.coin_unit as  CoinUnit,
                                c.coin_weight as CoinWeight,
                                c.coin_isexcavate as CoinIsExcavate,
                                c.coin_sort as CoinSort,
                                c.coin_Algorithm as AlgorithmId
                                 from  coin_type as c
                                where c.id=@Id";
            return _conn.QuerySingleOrDefault<CoinInfo>(sql, new { Id = id });
        }

        public IEnumerable<CoinInfoDto> GetCoinInfos()
        {
            string sql = @"select
                                c.id as Id,
                                c.coin_name as CoinName,
                                c.coin_device as CoinDevice,
                                c.coin_short as CoinShort,
                                c.coin_time as CoinTime,
                                c.coin_unit as  CoinUnit,
                                c.coin_weight as CoinWeight,
                                c.coin_isexcavate as CoinIsExcavate,
                                c.coin_sort as CoinSort,
                                ca.`Algorithm` as CoinAlgorithm
                                 from  coin_type as c
                                inner join coin_algorithm as ca on c.coin_Algorithm=ca.id where c.isdel=0";
            return _conn.Query<CoinInfoDto>(sql);

        }
        public IEnumerable<CoinInfoDto> GetCoinInfosByPoolId(int poolId)
        {
            string sql = @"select
                                c.id as Id,
                                c.coin_name as CoinName,
                                c.coin_device as CoinDevice,
                                c.coin_short as CoinShort,
                                c.coin_time as CoinTime,
                                c.coin_unit as  CoinUnit,
                                c.coin_weight as CoinWeight,
                                c.coin_isexcavate as CoinIsExcavate,
                                c.coin_sort as CoinSort,
                                ca.`Algorithm` as CoinAlgorithm
                                from  pool_coin as pc
								inner join coin_type as c on c.id=pc.coinid
                                inner join coin_algorithm as ca on c.coin_Algorithm=ca.id
								where c.isdel=0 and pc.poolid=@PoolId";
            return _conn.Query<CoinInfoDto>(sql,new { PoolId= poolId });

        }
        /// <summary>
        /// 包括删除的
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CoinInfoDto> GetAllCoinInfos()
        {
            string sql = @"select
                                c.id as Id,
                                c.coin_name as CoinName,
                                c.coin_device as CoinDevice,
                                c.coin_short as CoinShort,
                                c.coin_time as CoinTime,
                                c.coin_unit as  CoinUnit,
                                c.coin_weight as CoinWeight,
                                c.coin_isexcavate as CoinIsExcavate,
                                c.coin_sort as CoinSort,
                                ca.`Algorithm` as CoinAlgorithm
                                 from  coin_type as c
                                inner join coin_algorithm as ca on c.coin_Algorithm=ca.id";
            return _conn.Query<CoinInfoDto>(sql);

        }

        public bool Update(CoinInfo coinInfoInput)
        {
            string sql = @"update coin_type set 
                                    coin_name=@CoinName,
                                    coin_device=@CoinDevice,
                                    coin_short=@CoinShort,
                                    coin_time=@CoinTime,
                                    coin_unit=@CoinUnit,
                                    coin_weight=@CoinWeight,
                                    coin_isexcavate=@CoinIsExcavate,
                                    coin_sort=@CoinSort,
                                    coin_Algorithm=@AlgorithmId
                                    where id=@Id";

            return _conn.Execute(sql, coinInfoInput) > 0;
        }

        public bool Delete(int[] ids)
        {
            string sql = "update coin_type set isdel=1 where id in @Ids";
            return _conn.Execute(sql, new { ids }) > 0;
        }

        public IEnumerable<CoinInfoDto> Search(CoinInfoCondition infoCondition)
        {
            string tatolCountSql = @"select count(0) from coin_type as c
                                                      inner join coin_algorithm as ca on c.coin_Algorithm=ca.id where c.isdel=0";
            string sql = @"select 
                                c.id as Id,
                                c.coin_name as CoinName,
                                c.coin_device as CoinDevice,
                                c.coin_short as CoinShort,
                                c.coin_time as CoinTime,
                                c.coin_unit as  CoinUnit,
                                c.coin_weight as CoinWeight,
                                c.coin_isexcavate as CoinIsExcavate,
                                c.coin_sort as CoinSort,
                                ca.`Algorithm` as CoinAlgorithm
                                 from  coin_type as c
                                inner join coin_algorithm as ca on c.coin_Algorithm=ca.id where c.isdel=0";
            if (!string.IsNullOrEmpty(infoCondition.Algorithm))
            {
                string addSql = " and ca.`Algorithm` like CONCAT('%',@Algorithm,'%')";
                sql += addSql;
                tatolCountSql += addSql;
            }
            sql += " limit @PageIndex,@PageSize";
            try
            {
                infoCondition.TotalCount = _conn.QuerySingleOrDefault<int>(tatolCountSql, new { infoCondition.Algorithm });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return _conn.Query<CoinInfoDto>(sql, new { infoCondition.Algorithm, PageIndex = (infoCondition.PageIndex - 1) * infoCondition.PageSize, infoCondition.PageSize });
        }

    }
}
 